package org.openkoala.koala.deploy.action;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.openkoala.koala.console.ConsoleFactory;
import org.openkoala.koala.deploy.dialog.FileOpenDialog;
import org.openkoala.koala.deploy.ejb.pojo.EJBDeploy;
import org.openkoala.koala.deploy.ejb.util.EJBDeployCreate;
import org.openkoala.koala.deploy.tree.EJBSelectionUI;
import org.openkoala.koala.deploy.tree.SelectionUI;
import org.openkoala.koala.exception.KoalaException;
import org.openkoala.koala.maven.MavenExcuter;
import org.openkoala.koala.popup.BaseAction;
import org.openkoala.koala.util.ProjectQueryUtil;

public class EJBDeployAction extends BaseAction {

	EJBDeploy ejbDeploy;

	String result;

	String selectedPath;

	public EJBDeployAction() {

	}

	public void process() {
		try {
			ProgressMonitorDialog monitorDialog = new ProgressMonitorDialog(shell);
			monitorDialog.run(true, true, new IRunnableWithProgress() {
				@Override
				public void run(IProgressMonitor monitor)
						throws InvocationTargetException, InterruptedException {
					monitor.beginTask("处理及打包EJB...", 2);
					monitor.worked(1);
					monitor.setTaskName("打包EAR...");
					try {
						result = EJBDeployCreate.process(ejbDeploy);
					} catch (KoalaException e) {
						e.printStackTrace();
						MessageDialog.openError(shell,
								"ERROR", e.getMessage());
					}
					monitor.worked(2);
					monitor.done();
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
			
		}
		FileOpenDialog dialog = new FileOpenDialog(new Shell(Display.getCurrent(),SWT.CLOSE), "EJB服务部署",result);
		dialog.open();
	}

	@Override
	public void run(IAction action) {
		ConsoleFactory.showConsole();
		selectedPath = getResource().getLocationURI().getPath();
		try {
			if (ProjectQueryUtil.isParentProject(selectedPath) == false) {
				selectedPath = selectedPath.substring(0,
						selectedPath.lastIndexOf("/"));
			}
		} catch (KoalaException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		try {
			ProgressMonitorDialog monitorDialog = new ProgressMonitorDialog(shell);
			monitorDialog.run(true, true, new IRunnableWithProgress() {
				@Override
				public void run(IProgressMonitor monitor)
						throws InvocationTargetException, InterruptedException {
					monitor.beginTask("分析中...", 4);
					monitor.worked(1);
					monitor.setTaskName("检查原项目的正确性");
					try {
						MavenExcuter.runMaven(selectedPath);
						monitor.worked(2);
						monitor.setTaskName("分析项目所拥有的接口及实现...");
						ejbDeploy = EJBDeployCreate.analytics(selectedPath);
					} catch (KoalaException e) {
						e.printStackTrace();
						MessageDialog.openError(shell,
								"ERROR", e.getMessage());
					}
					monitor.done();
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}

		SelectionUI ejbUi = new EJBSelectionUI(new Shell(Display.getDefault()),
				this);
		ejbUi.initData(ejbDeploy);
		ejbUi.open();
	}

	public void setEjbDeploy(EJBDeploy ejbDeploy) {
		this.ejbDeploy = ejbDeploy;
	}

}
